package edu.towson.cosc.cosc455.jyoun.project1;

import java.util.ArrayList;
import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

public class MyLexicalAnalyzer implements LexicalAnalyzer {
	 ArrayList<String> htmlList = new ArrayList<String>();
     int currentPos;
     boolean end;
     String token;
     char thischar;
     private static String htmlsf;
     public void thishtml(String line) throws CompilerException{
    	htmlsf= line;
 		currentPos = 0;
 		end = false;
 		
 		htmlList.add("#START DOCUMENT");
        htmlList.add("#END DOCUMENT");
        htmlList.add("#START TITLE");
        htmlList.add("#END TITLE");
        htmlList.add("#START HEAD");
        htmlList.add("#END HEAD");
        htmlList.add("#START PARAGRAPH");
        htmlList.add("#END PARAGRAPH");
        htmlList.add("#START BOLD");
        htmlList.add("#END BOLD");
        htmlList.add("#START ITALICS");
        htmlList.add("#END ITALICS");
        htmlList.add("#START LIST");
        htmlList.add("#END LIST");
        htmlList.add("#START ITEM");
        htmlList.add("#END ITEM");
        htmlList.add("#NEWLINE");
        htmlList.add("#START AUDIO");
        htmlList.add("#END AUDIO");
        htmlList.add("#START VIDEO");
        htmlList.add("#END VIDEO");
        htmlList.add("#START DEFINE");
        htmlList.add("#NAME");
        htmlList.add("#VALUE");
        htmlList.add("#END DEFINE");
        htmlList.add("#START USE");
        htmlList.add("#END USE");
 		
 		getCharacter();
 		getNextToken();
 	}
	@Override
	public void getNextToken(){
		if (end) {
			MyCompiler.token = null;
			return;
		}
		
		token = "";
		
		if(thischar != '#' && thischar != '\n' && thischar != '\t' && !isSpace(thischar)) {
			while(thischar != '#' && !end) {
				if(thischar != '\n' && thischar != '\t') {
					addCharacter();
					getCharacter();
				}
				else
					getCharacter();
			}
			
			MyCompiler.token = token;
		}
		
		else if(thischar == '\n' || thischar == '\t' || isSpace(thischar)){
			getCharacter();
			getNextToken();
		}
			
		
		else {	
			while(!isSpace(thischar) && !end) {
				addCharacter();
				getCharacter();
			}
			
			if(lookupToken()){
				MyCompiler.token = token;
				
				if(thischar == ' ') 
					getCharacter();
			}
			
			else if(token.equalsIgnoreCase("#Start") || token.equalsIgnoreCase("#End")) {
				addCharacter();
				getCharacter();
				
				while(!isSpace(thischar) && !end) {
					addCharacter();
					getCharacter();
				}
				
				if(lookupToken()){
					MyCompiler.token = token;
					
					if(thischar == ' ') 
						getCharacter();
				}
				else {
					String m = token + " is an invalid token.";
					System.out.print(m);
				}
			}
			
			else {
				String m = token + " is an invalid token.";
				System.out.print(m);
			}
		}
	}

	@Override
	public void getCharacter() {
		if (currentPos < htmlsf.length()) 
			thischar = htmlsf.charAt(currentPos++);
		else {
			end = true;
			return;
			}
		}
	
	

	@Override
	public void addCharacter() {
		token += thischar;

	}
	
	public boolean isSpace(char c) {
        // TODO Auto-generated method stub
        if (c == ' '){
        	return true;
        }
        return false;
}
	


	@Override
	public boolean lookupToken() {
		 if(htmlList.contains(token))
        	  return true;
          return false;
      }
	}

